package com.qfedu.fmmall.service.impl;

import com.qfedu.fmmall.dao.ProductImgMapper;
import com.qfedu.fmmall.dao.ProductMapper;
import com.qfedu.fmmall.dao.ProductParamsMapper;
import com.qfedu.fmmall.dao.ProductSkuMapper;
import com.qfedu.fmmall.entity.*;
import com.qfedu.fmmall.service.ProductService;
import com.qfedu.fmmall.vo.ResStatus;
import com.qfedu.fmmall.vo.ResultVO;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;

/**
 * @author liuyadi
 */
@Service
public class ProductServiceImpl implements ProductService {

    private ProductMapper productMapper;
    private ProductImgMapper productImgMapper;
    private ProductSkuMapper productSkuMapper;
    private ProductParamsMapper productParamsMapper;

    public ProductServiceImpl(ProductMapper productMapper, ProductImgMapper productImgMapper, ProductSkuMapper productSkuMapper, ProductParamsMapper productParamsMapper) {
        this.productMapper = productMapper;
        this.productImgMapper = productImgMapper;
        this.productSkuMapper = productSkuMapper;
        this.productParamsMapper = productParamsMapper;
    }

    @Override
    public ResultVO listRecommendProducts() {
        List<ProductVO> productVOS = productMapper.queryRecommendProducts();
        return new ResultVO(ResStatus.OK.getCode(), "success", productVOS);
    }

    @Override
    public ResultVO getProductBasicInfo(String productId) {
        // 1.商品的基本信息
        // 1.1 创建 example 查询条件
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        //状态为1表示上架商品
        criteria.andEqualTo("productStatus", 1);
        List<Product> products = productMapper.selectByExample(example);
        if (products.size() > 0) {
            // 2.商品图片
            Example example1 = new Example(ProductImg.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("itemId", productId);
            List<ProductImg> productImgs = productImgMapper.selectByExample(example1);
            // 3.商品套餐
            Example example2 = new Example(ProductSku.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andEqualTo("productId", productId);
            criteria2.andEqualTo("status", 1);
            List<ProductSku> productSkus = productSkuMapper.selectByExample(example2);
            // 集合初始化时，尽量指定集合初始值大小
            HashMap<String, Object> basicInfo = new HashMap<>();
            basicInfo.put("product", products);
            basicInfo.put("productImg", productImgs);
            basicInfo.put("productSku", productSkus);
            return new ResultVO(ResStatus.OK.getCode(), "success", basicInfo);
        } else {
            return new ResultVO(ResStatus.NO.getCode(), "查询商品不存在", null);
        }
    }

    @Override
    public ResultVO getProductParamsById(String productId) {
        // 根据商品id查询商品参数
        Example example = new Example(ProductParams.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        List<ProductParams> productParams = productParamsMapper.selectByExample(example);
        if (productParams.size() > 0) {
            return new ResultVO(ResStatus.OK.getCode(), "", productParams);
        } else {
            return new ResultVO(ResStatus.NO.getCode(), "该商品为三无商品！", null);
        }
    }
}
